package cn.chengq.misc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 克隆对象时，被克隆对象的类需要实现Cloneable接口(标识接口)。而且实现的是浅拷贝。
 */
public class Student implements Cloneable {

    public String name;
    public int age;
    public String gender;
    public List<String> arms;

    public Student() {
        this(null, 0, null);
    }

    public Student(String name, int age, String gender, String... arms) {
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.arms = new ArrayList<String>();
        this.arms.addAll(Arrays.asList(arms));
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public String getGender() {
        return gender;
    }

    public List<String> getArms() {
        return arms;
    }

    public void setArms(List<String> arms) {
        this.arms = arms;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", arms=" + arms +
                '}';
    }

    public static void main(String[] args) {
        Student s1 = new Student("孙悟空", 520, "男");
        try {
            System.out.println(s1);
            s1.getArms().add("金箍棒");
            Student s2 = (Student) s1.clone();
            System.out.println(s2);

            // 变更s1的属性
            s1.setName("猪八戒");
            s1.setAge(1000);
            s1.setGender("女");
            System.out.println(s1);
            System.out.println(s2);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
